<template>
  <div>
    <el-dialog
      :title="info.issAdd?'角色编辑':'角色添加'"
      :visible.sync="info.isshow"
      @closed="closed"
      width="60%"
    >
      <el-form :model="form">
        {{form}}
        <el-form-item class="con" label="角色名称" label-width="120px">
          <el-input placeholder="请输入内容" v-model="form.rolename"></el-input>
        </el-form-item>
        <!-- 树形结构 -->
        <el-form-item class="con" label="角色权限" label-width="120px">
          <el-tree
            :data="menulist"
            :props="{children: 'children',label: 'title',}"
            show-checkbox
            node-key="id"
            ref="tree"
          ></el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { successAlert } from "../../../utils/alert";
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../request/api";
import {mapGetters,mapActions} from "vuex"
export default {
  components: {},
  computed:{
    ...mapGetters({
      user:"info"
    })
  },
  mounted() {
    //挂载请求菜单列表数据--并赋值menulist
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.menulist = res.data.list;
      }
    });
  },
  // 接收父节组件参数
  props: ["list", "info"],
  //初始化数据
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: 2,//默认值---后端要求
      },
      menulist: [],
    };
  },
  methods: {
    ...mapActions({
      changeInfo:"changeInfo"
    }),
    // 点击取消
    cancel() {
      this.info.isshow = false;
    },
    //清空数据
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: "",
      };
      //清空树
      this.$refs.tree.setCheckedKeys([]);
    },
    // 点击添加
    add() {
      //获取树的checked的id
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹提示
          successAlert(res.data.msg);
          // 弹框消失
          this.cancel();
          //清空数据
          this.empty();
          //刷新列表
          this.$emit("init");
        }

      });
    },
    getOne(id) {
      reqroleinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
        this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
      });
    },
    // form关闭
    closed() {
      if(!this.info.isAdd){
        this.empty()
      }
    },
    //点击修改
    update() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.form).then((res) => {
        if (res.data.code == 200) {
           successAlert(res.data.msg);
          //如果修改的角色的id是当前用户的roleid，就需要重新登陆
          if(this.form.id===this.user.roleid){
            this.changeInfo({})
            this.$router.replace("/login")
            return;
          }

          this.$emit("init");
          this.cancel();
          this.empty();
         
        }
      });
    },
  },
};
</script>
<style lang="less">
.fl {
  float: left;
  width: 100px;
}
.el-input {
  float: left;
  width: 400px;
}
.con {
  overflow: hidden;
}
.el-dialog__header {
  width: 50%;
}
</style>